<!DOCTYPE html>
<html>

<head>
    <style>
        /* 移动端样式 */
        @media screen and (max-width: 750px) {
            .vt-login {
                height: calc(100% - 74px);
                --item-width: 320px;
            }
        }

        /* PC端样式 */
        @media screen and (min-width: 750px) {
            .vt-login {
                height: calc(100% - 34px);
                --item-width: 320px;
            }
        }

        .login-container {
            display: flex;
            /* 水平居中 */
            justify-content: center;
            /* 垂直居中 */
            align-items: center;
        }

        .login-container>form {
            width: var(--item-width);
        }

        .login-logo {
            width: var(--item-width);
        }

        .login-container .login-title {
            text-align: center;
            font-size: 22px;
            font-weight: bold;
            color: var(--primary-color);
        }

        .login-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }
    </style>
</head>

<body>
    <div class="vt-login login-container">
        <form class="layui-form">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div style="text-align: center;">
                        <img class="login-logo" src="/logo.svg" />
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="login-title">
                        Vita 后台管理系统
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="admin" lay-verify="required" placeholder="用户名" autofocus
                        lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" value="aday.fun" lay-verify="required" placeholder="密&emsp;码"
                        lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item layui-hide">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-key"></i>
                    </div>
                    <input type="text" name="totp" placeholder="动态口令" lay-reqtext="请填写动态口令" autocomplete="off"
                        class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item layui-hide">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-vercode"></i>
                            </div>
                            <input type="text" name="captcha" value="" placeholder="验证码" lay-reqtext="请填写验证码"
                                autocomplete="off" class="layui-input" lay-affix="clear">
                        </div>
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <a href="javascript:;"><img id="captchaImg" lay-on="captcha"></a>
                            <a href="javascript:;" style="float: right; margin-top: 7px; margin-right: 7px;"
                                lay-on="captcha">获取验证码</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
                <a href="javascript:;" style="float: right; margin-top: 7px;">忘记密码？</a>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login-btn">登录</button>
            </div>
            <div class="layui-form-item">
                <label style="float: left; margin-top: 10px; margin-right: 10px;">登录方式：</label>
                <span>
                    <input type="radio" name="loginMethod" value="password" title="密码"
                        lay-filter="radio-login-method-filter" checked>
                    <input type="radio" name="loginMethod" value="totp" title="动态口令" disabled
                        lay-filter="radio-login-method-filter">
                </span>
            </div>
            <div class="layui-form-item login-other">
                <label>社交账号登录</label>
                <span style="padding: 0 21px 0 6px;">
                    <a href="javascript:;"><i class="layui-icon layui-icon-login-qq" style="color: #3492ed;"></i></a>
                    <a href="javascript:;"><i class="layui-icon layui-icon-login-wechat"
                            style="color: #4daf29;"></i></a>
                    <a href="javascript:;"><i class="layui-icon layui-icon-login-weibo" style="color: #cf1900;"></i></a>
                </span>
                <span>或 <a href="#reg">注册帐号</a></span>
            </div>
        </form>
    </div>

    <div class="layui-footer"></div>

    <script type="module">
        import { admin } from "@/scripts/admin.js";
        import { useLogin } from "@/api/login.js";

        let $ = layui.$;
        let form = layui.form;
        let layer = layui.layer;
        let util = layui.util;

        admin.loadFooter();

        let initCaptcha = function () {
            useLogin.captchaEnabled().then(function (enabled) {
                if (enabled) {
                    // closest(): 筛选第一个符合条件的父元素
                    $("input[name='captcha']").attr("lay-verify", "required").closest(".layui-form-item").removeClass("layui-hide");
                }
            });
        }

        initCaptcha();
        form.render();

        // 提交事件
        form.on('submit(login-btn)', function (data) {
            // 登录
            admin.login(data.field);
            // 阻止默认 form 跳转
            return false;
        });

        form.on('radio(radio-login-method-filter)', function (data) {
            let value = data.elem.value;
            $("input[name='password']").removeAttr("lay-verify").closest(".layui-form-item").addClass("layui-hide");
            $("input[name='totp']").removeAttr("lay-verify").closest(".layui-form-item").addClass("layui-hide");

            $(`input[name='${value}']`).attr("lay-verify", "required").closest(".layui-form-item").removeClass("layui-hide");
        });

        util.on({
            captcha: function () {
                useLogin.captcha().then((base64) => {
                    $("#captchaImg").attr('src', base64).parent().next().addClass("layui-hide");
                });
            },
        });


    </script>


</body>

</html>